package com.wzu.dao;

import com.wzu.pojo.Reader;
import com.wzu.utils.JDBCUtils;
import java.util.List;
import java.util.ArrayList;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReaderDaoImpl implements ReaderDao {

    @Override
    public Reader getReaderByUserId(int userId) {
        String sql = "SELECT * FROM readers WHERE user_id = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    Reader reader = new Reader();
                    reader.setReader_id(rs.getInt("reader_id"));
                    reader.setUserId(rs.getInt("user_id")); // 别忘了设置 user_id
                    reader.setName(rs.getString("name"));
                    reader.setGender(rs.getString("gender"));
                    reader.setAge(rs.getInt("age"));
                    reader.setContact_info(rs.getString("contact_info"));
                    reader.setId_type(rs.getString("id_type"));
                    reader.setId_number(rs.getString("id_number"));
                    reader.setBorrow_limit(rs.getInt("borrow_limit"));
                    reader.setBorrow_period(rs.getInt("borrow_period"));
                    reader.setCan_renew(rs.getBoolean("can_renew"));
                    return reader;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public int updateReader(Reader reader) {
        String sql = "UPDATE readers SET name =?, gender =?, age =?, contact_info =?, id_type =?, id_number =?, borrow_limit =?, borrow_period =?, can_renew =? WHERE reader_id =?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, reader.getName());
            ps.setString(2, reader.getGender());
            ps.setInt(3, reader.getAge());
            ps.setString(4, reader.getContact_info());
            ps.setString(5, reader.getId_type());
            ps.setString(6, reader.getId_number());
            ps.setInt(7, reader.getBorrow_limit());
            ps.setInt(8, reader.getBorrow_period());
            ps.setBoolean(9, reader.isCan_renew());
            ps.setInt(10, reader.getReader_id());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    // 实现
    @Override
    public boolean addReader(Reader reader) {
        String sql = "INSERT INTO readers (reader_id, name, gender, age, contact_info, id_type, id_number, borrow_limit, borrow_period, can_renew) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, reader.getReader_id());
            ps.setString(2, reader.getName());
            ps.setString(3, reader.getGender());
            ps.setInt(4, reader.getAge());
            ps.setString(5, reader.getContact_info());
            ps.setString(6, reader.getId_type());
            ps.setString(7, reader.getId_number());
            ps.setInt(8, reader.getBorrow_limit());
            ps.setInt(9, reader.getBorrow_period());
            ps.setBoolean(10, reader.isCan_renew());
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    @Override
    public int insertReader(Reader reader) {
        String sql = "INSERT INTO readers (" +
                "user_id, name, gender, age, contact_info, " +
                "id_type, id_number, borrow_limit, borrow_period, can_renew" +
                ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, reader.getUserId());
            ps.setString(2, reader.getName());
            ps.setString(3, reader.getGender());
            ps.setInt(4, reader.getAge());
            ps.setString(5, reader.getContact_info());
            ps.setString(6, reader.getId_type());
            ps.setString(7, reader.getId_number());
            ps.setInt(8, reader.getBorrow_limit());
            ps.setInt(9, reader.getBorrow_period());
            ps.setBoolean(10, reader.isCan_renew());

            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
    @Override
    public List<Reader> getAllReaders() {
        List<Reader> readerList = new ArrayList<>();
        String sql = "SELECT * FROM readers";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {

            while (rs.next()) {
                Reader reader = new Reader();
                reader.setReader_id(rs.getInt("reader_id"));
                reader.setUser_id(rs.getInt("user_id"));
                reader.setName(rs.getString("name"));
                reader.setGender(rs.getString("gender"));
                reader.setAge(rs.getInt("age"));
                reader.setContact_info(rs.getString("contact_info"));
                reader.setId_type(rs.getString("id_type"));
                reader.setId_number(rs.getString("id_number"));
                reader.setBorrow_limit(rs.getInt("borrow_limit"));
                reader.setBorrow_period(rs.getInt("borrow_period"));
                reader.setCan_renew(rs.getBoolean("can_renew"));
                readerList.add(reader);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return readerList;
    }
}